package com.king.cms.common;

import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener {
	 ServletContext application = null;
     
	 public HttpSessionBinding(ServletContext application)
	 {
	   super();
	   if (application ==null)
	     throw new IllegalArgumentException("Null application is not accept.");
	   
	   this.application = application;
	   
	 }

	 public void valueBound(javax.servlet.http.HttpSessionBindingEvent e) 
	 { 
	   Vector activeSessions = (Vector) application.getAttribute("activeSessions");
	   if (activeSessions == null)
	   {
	     activeSessions = new Vector();
	   }

	   UserObj sessionUser = (UserObj)e.getSession().getAttribute("user");
	   if (sessionUser != null)
	   {
	     activeSessions.add(e.getSession());
	   }
	   application.setAttribute("activeSessions",activeSessions);
	 }

	 public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e) 
	 {
		 try{  
			    HttpSession session = e.getSession();
			    if(session != null){
			    	UserObj sessionUser = (UserObj)session.getAttribute("user"); 
				    if (sessionUser == null)
				    {
					     Vector activeSessions = (Vector) application.getAttribute("activeSessions");
					     if (activeSessions != null)
					     {
					       activeSessions.remove(e.getSession().getId());
					       application.setAttribute("activeSessions",activeSessions);
					     }
				   }
			    }
		 }catch(Exception ee){
			  
		 }
	 }
	}
 
